package com.fjec.user.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.DateUtil;
import com.fjec.user.entity.SysRole;
import com.fjec.user.mapper.SysRoleMapper;
import com.fjec.user.service.ISysRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 003.角色表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-08-31
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Resource
    private SysRoleMapper mapper;

    @Override
    public List<String> getRoleCodesByOpenId(String openId) {
        return mapper.getRoleCodesByOpenId(openId);
    }

    @Override
    public List<String> getRoleCodesByUserName(String userName) {
        return mapper.getRoleCodesByUserName(userName);
    }

    @Override
    public List<String> getRoleCodesByUserId(Long id) {
        return mapper.getRoleCodesByUserId(id);
    }

    @Override
    public String saveUserRole(Long userId, String roleCode) {
        int saved = mapper.saveUserRole(userId, roleCode);
        if (saved == 1)
            return "success";
        else
            return "failed";
    }

    @Override
    public JsonResult saveRole(SysRole sysRole) {
        sysRole.setDelFlag(0);
        LocalDateTime time = DateUtil.nowLdt();
        sysRole.setCreateTime(time);
        sysRole.setUpdateTime(time);
        boolean saved = this.save(sysRole);
        if (saved)
            return JsonResult.SUCCESS;
        else
            return JsonResult.FAILURE;
    }

    @Override
    public JsonResult getRolesPage(Page page) {
        page = this.page(page);
        return JsonResult.builder()
                .data(page.getRecords())
                .total(page.getTotal())
                .build();
    }
}
